package com.yl.plugin.method;

import com.yl.plugin.method.base.BaseMethod;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuxubo
 */
public class DetailMethod implements BaseMethod {
    @Override
    public int order() {
        return 2;
    }

    @Override
    public Method buildMethod(IntrospectedTable introspectedTable) {
        JavaVisibility methodVisibility = JavaVisibility.PUBLIC;

        // 设置返回类型：实体类类型
        FullyQualifiedJavaType methodReturnType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

        List<Parameter> methodParameterList = new ArrayList<Parameter>();
        //主键列表
        for (IntrospectedColumn column : introspectedTable.getPrimaryKeyColumns()){
            String javaProperty = column.getJavaProperty();
            FullyQualifiedJavaType javaType = column.getFullyQualifiedJavaType();
            // 参数类型
            FullyQualifiedJavaType methodParameterType = new FullyQualifiedJavaType(javaType.getShortName());
            // 方法参数
            Parameter methodParameter = new Parameter(methodParameterType, javaProperty);
            methodParameterList.add(methodParameter);
        }

        Method method = new Method("detail");
        method.setVisibility(methodVisibility);
        method.setReturnType(methodReturnType);
        methodParameterList.forEach(pm -> method.addParameter(pm));

        method.addJavaDocLine("/**");
        method.addJavaDocLine(" * 详情");
        method.addJavaDocLine(" * ");
        methodParameterList.forEach(pm ->  method.addJavaDocLine(" * @param " + pm.getName()));
        method.addJavaDocLine(" */");


        return method;
    }

}
